<template>
  <!-- 双选会二维码统一解析和跳转页面 -->
  <page-wrap
    :bgColor="dynamicColor"
    txtColor="#222"
    :showTop="false"
    :dark="false"
    :hideSafeArea="true"
    hideTab
  >
    <view class="img-container">
      <image :src="imgUrl" class="img"></image>
      <view class="footer">
        <view :class="['btn']" @click="handleReturn">去体验职途求职工具</view>
      </view>
    </view>
    <LoginDialog
      ref="loginDialog"
      @loginSuccessBack="loginSuccessBack"
    ></LoginDialog>
  </page-wrap>
</template>

<script>
import {
  getOneQRInfo,
  cardRegist,
  getCardPersonRightNum,
  getPersonOneRight,
  addBuryingPoint,
} from '@/api/modules/career';
import { env } from '@/api/root';
import LoginDialog from '@/components/LoginDialog';
export default {
  name: 'toolsShareParse', //原来dataCenterParse里的文件，先移动防止代码回滚
  components: {
    LoginDialog,
  },
  data() {
    return {
      dynamicColor: 'transparent',
      navHeight: getApp().globalData.navHeight,
      imgUrl:
        'https://img3.job1001.com/ypMp/elan/images/career/careerPathDesc.jpg',
      personId: uni.getStorageSync('person-id').slice(2) || undefined,
      roleType: '',
      rightId: '',
      cardType: '',
      valueId: '',
      fromPersonId: '',
      userInfo: '',
      discLeftUseNum: 0,
      careerAssessmentId: '',
    };
  },
  async onLoad(options) {
    this.showLoading();
    if (options.scene) {
      let paramStr = decodeURIComponent(options.scene);
      let resObj = {};
      if (paramStr) {
        paramStr.split('&') &&
          paramStr.split('&').forEach((item) => {
            resObj[item.split('=')[0]] = item.split('=')[1];
          });
      }
      if (resObj.hasOwnProperty('cardType')) {
        //兼容以前生成的二维码
        this.cardType = resObj.cardType;
        uni.setStorageSync('cardType', this.cardType);
      }
      if (resObj.hasOwnProperty('ct')) {
        this.cardType = resObj.ct;
        uni.setStorageSync('cardType', this.cardType);
      }
      if (resObj.hasOwnProperty('role')) {
        this.roleType = resObj.role; //10代表学生，20代表老师
        uni.setStorageSync('roleType', this.roleType);
      }
      if (resObj.hasOwnProperty('rightId')) {
        this.rightId = resObj.rightId;
      }
    } else if (options.hasOwnProperty('ct')) {
      this.cardType = resObj.cardType;
      uni.setStorageSync('cardType', this.cardType);
      this.cardType = options.ct;
      uni.setStorageSync('cardType', this.cardType);
      this.rightId = options.rightId;
      this.roleType = options.role; //10代表学生，20代表老师
      uni.setStorageSync('roleType', this.roleType);
      this.rightId = resObj.rightId;
    }
    await this.handleBeforeJump();
  },
  onHide() {
    uni.hideLoading();
  },
  methods: {
    async handleBeforeJump() {
      if (!this.personId) {
        this.showLoginDialog();
        return;
      }
      const res = await this.cardRegistFn();
      if (res.code == 200) {
        await this.handleCopyRight();
      } else {
        uni.showToast({
          title: res.status_desc,
          icon: 'none',
        });
      }
    },
    // 第一步：换绑卡
    async cardRegistFn() {
      const params = {
        personId: this.personId,
        cardType: this.cardType,
        force: 1, //  强行绑卡 ，会触发换绑操作,一定成功
      };
      const res = await cardRegist(params);
      return res;
    },
    // 第二步：根据不同权益处理
    async handleCopyRight() {
      switch (+this.rightId) {
        case 1: // 性格测评 1
          if (this.roleType == 20) {
            this.addBuryingPointFn(40005);
          } else {
            this.addBuryingPointFn(40019);
          }
          await this.getCardPersonRightNumFn();
          await this.copyRightFn();
          break;
        case 2: // 职业测评 2
          if (this.roleType == 20) {
            this.addBuryingPointFn(40006);
          } else {
            this.addBuryingPointFn(40020);
          }
          await this.getCardPersonRightNumFn();
          await this.handleCareerAssessmentFn();
          break;
        case 4: // 简历诊断 4
          if (this.roleType == 20) {
            this.addBuryingPointFn(40003);
          } else {
            this.addBuryingPointFn(40017);
          }
          this.jumpToPage(
            '/pagesSub/AIResumeDiagnoseNew/index?fromSxh=fromSxhQrCode'
          );
          break;
        case 7: // 职途报告 7
          if (this.roleType == 20) {
            this.addBuryingPointFn(40007);
          } else {
            this.addBuryingPointFn(40021);
          }
          this.jumpToPage(
            `/pagesSub/userCenter/careerPathV2/components/CreateAndDeliverCareerPathReport?fromSxh=fromSxhQrCode`
          );
          break;
        case 17: // AI面评  17
          if (this.roleType == 20) {
            this.addBuryingPointFn(40004);
          } else {
            this.addBuryingPointFn(40018);
          }
          this.jumpToPage('/pagesSub/companyCenter/interviewAI/majorSearchAI'); // 列表页
          break;
        default:
          break;
      }
    },
    // 获取个人权益剩余次数
    async getCardPersonRightNumFn() {
      const params = {
        personId: this.personId || undefined,
        rightId: this.rightId,
        cardType: this.cardType,
      };
      const res = await getCardPersonRightNum(params);
      if (res.code == 200) {
        this.discLeftUseNum = res.data.ownNum - res.data.useNum;
        console.log('discLeftUseNum', this.discLeftUseNum);
      }
    },
    // 复制性格测评数据
    async copyRightFn() {
      if (this.discLeftUseNum > 0) {
        let url = '';
        if (env == 'test') {
          url = `https://3gtest.crm1001.com/disc/#/disc/question?personId=${this.personId}&iname=${this.personId}&cardType=${this.cardType}&phone=18825449087&from=miniprogramV2&discLeftUseNum=${this.discLeftUseNum}&cardType=${this.cardType}`;
        } else if (env == 'pro' || env == 'pre') {
          url = `https://3g.crm1001.com/v2/disc/#/disc/question?personId=${this.personId}&iname=${this.personId}&cardType=${this.cardType}&phone=18825449087&from=miniprogramV2&discLeftUseNum=${this.discLeftUseNum}&cardType=${this.cardType}`;
        }
        uni.reLaunch({
          url:
            '/pagesSub/public/web/descWebView?link=' + encodeURIComponent(url),
        });
      } else {
        getPersonOneRight({
          rightId: this.rightId,
          personId: this.personId,
        }).then((res) => {
          if (res.code == 200) {
            const nowRightItem = res.data;
            const userId = nowRightItem.extra && nowRightItem.extra.userId;
            let url = '';
            if (userId) {
              if (env == 'test') {
                url = `https://3gtest.crm1001.com/disc/#/disc/result?userId=${userId}&personId=${this.personId}&iname=${this.personId}&phone=18825449087&from=miniprogramV2&discLeftUseNum=${this.discLeftUseNum}&cardType=${this.cardType}`;
              } else if (env == 'pro' || env == 'pre') {
                url = `https://3g.crm1001.com/v2/disc/#/disc/result?userId=${userId}&personId=${this.personId}&iname=${this.personId}&phone=18825449087&from=miniprogramV2&discLeftUseNum=${this.discLeftUseNum}&cardType=${this.cardType}`;
              }
              uni.reLaunch({
                url:
                  '/pagesSub/public/web/descWebView?link=' +
                  encodeURIComponent(url),
              });
            } else {
              if (env == 'test') {
                url = `https://3gtest.crm1001.com/disc/#/disc/question?personId=${this.personId}&iname=${this.personId}&cardType=${this.cardType}&phone=18825449087&from=miniprogramV2&discLeftUseNum=${this.discLeftUseNum}&cardType=${this.cardType}`;
              } else if (env == 'pro' || env == 'pre') {
                url = `https://3g.crm1001.com/v2/disc/#/disc/question?personId=${this.personId}&iname=${this.personId}&cardType=${this.cardType}&phone=18825449087&from=miniprogramV2&discLeftUseNum=${this.discLeftUseNum}&cardType=${this.cardType}`;
              }
              uni.reLaunch({
                url:
                  '/pagesSub/public/web/descWebView?link=' +
                  encodeURIComponent(url),
              });
            }
          } else {
            uni.reLaunch({ url: '/pagesSub/userCenter/careerPathV2/index' });
          }
        });
        // uni.navigateTo({
        //   url: `/pagesSub/careerNew/zhituPaytoC/index?rightId=1&cardType=${this.cardType}`,
        // });
      }
    },
    // 复制职业测评数据
    async handleCareerAssessmentFn() {
      if (this.discLeftUseNum > 0) {
        uni.navigateTo({
          url: `/pagesSub/career/activity/vocationalAssessment?personId=${this.personId}&fromWhere=careerPath`,
        });
      } else {
        uni.navigateTo({
          url: `/pagesSub/careerNew/zhituPaytoC/index?rightId=2&cardType=${this.cardType}`,
        });
      }
    },
    //埋点
    async addBuryingPointFn(etId) {
      if (!etId) {
        return;
      }
      const params = {
        etId,
        personId: uni.getStorageSync('person-id').slice(2) || '',
        cardType: this.cardType,
        jumpSource: 10,
        sourceType: 10,
      };
      console.log('params让我看看你是个什么东西', params);
      const res = await addBuryingPoint(params);
    },
    jumpToPage(url) {
      uni.navigateTo({ url });
    },
    showLoginDialog() {
      this.$refs.loginDialog.setVisibility(true);
    },
    handleReturn() {
      uni.reLaunch({ url: '/pagesSub/userCenter/careerPathV2/index' });
    },
    loginSuccessBack() {
      this.personId = uni.getStorageSync('person-id').slice(2) || undefined;
      if (this.personId) {
        this.handleBeforeJump();
      }
    },
    showLoading() {
      uni.showLoading({
        title: '请稍后~',
        mask: true,
      });
      setTimeout(() => {
        uni.hideLoading();
      }, 3000);
    },
  },
};
</script>

<style lang="scss" scoped>
.img-container {
  .img {
    width: 100%;
    height: 100vh;
  }
  .footer {
    position: fixed;
    bottom: constant(safe-area-inset-bottom);
    bottom: env(safe-area-inset-bottom);
    left: 32rpx;
    width: calc(100vw - 64rpx);
    display: flex;
    justify-content: space-between;
    align-items: center;
    .btn {
      width: 100%;
      color: #fff;
      font-size: 32rpx;
      text-align: center;
      background: #fe7220;
      border-radius: 50rpx;
    }
  }
}
</style>
